<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms 
of the GNU General Public License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the 
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	Resource PAGING class (alphabetic) -- only used when ordering by creator or title
*****/
class PAGINGALPHA
{
// Constructor
	function PAGINGALPHA($db, $vars, $sqlTotal)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		if(array_key_exists('pagingStart', $this->vars))
		{
			$this->session->setVar('mywikindx_pagingStartAlpha', $this->vars['pagingStart']);
			$this->start = $this->vars['pagingStart'];
		}
		else
		{
			if($start = $this->session->getVar('mywikindx_pagingStartalpha'))
				$this->start = $start;
			else
				$this->start = 0; // index of $this->pagingArray
		}
		$this->listType = 'list';
	}
// grab paging either from default or from session
	function getPaging($sqlTotal, $order, $where)
	{
// last element is for creators or titles beginning with a non-English alphabetic character 
		$this->pagingArray = range('A', 'Z');
		array_push($this->pagingArray, '??');
		if($order == 'creator')
			array_unshift($this->pagingArray, '--'); // no creators
		$this->grabTotal($sqlTotal, $order);
		if(empty($this->pagingArray))
			return $sqlTotal;
		if(!array_key_exists($this->start, $this->pagingArray))
			$this->start = array_shift(array_keys($this->pagingArray));
		if($order == 'title')
		{
			if($this->start > 26)
				$this->start = 0;
			if($this->start == 26) // '--'  force this to 26th element and display all non-alpha titles at the end
				$condition = " UPPER(REPLACE(REPLACE(REPLACE(REPLACE(" . 
				$this->db->tidyInputClause('title') . ", '{', ''), '}', '') 
				, '\"', ''), '\'', '')" . ") REGEXP '^[^A-Z]' ";
			else
				$condition = ' UPPER(' . $this->db->formatField('title') . ') LIKE ' . 
					$this->db->tidyInput($this->pagingArray[$this->start] . '%');
		}
		else if($order == 'creator')
		{
			if($this->start == 0) // '--'  non-alpha initial creator1 surname or NULL creator1 and creator2
				$condition = '(' . $this->db->formatField('creator1') . ' IS NULL AND ' . 
				$this->db->formatField('creator2') . ' IS NULL)';
			else if($this->start == 27) // '--'  non-alpha initial creator1 surname or NULL creator1 and creator2
				$condition = '(UPPER(' . $this->db->formatField('surname') . ") REGEXP '^[^A-Z]') ";
			else
				$condition = ' UPPER(' . $this->db->formatField('surname') . ') LIKE ' . 
					$this->db->tidyInput($this->pagingArray[$this->start] . '%');
		}
		if($where)
		{
			if(strpos($sqlTotal, 'GROUP BY') !== FALSE)
				$sqlTotal = str_replace(' GROUP BY ', ' AND ' . $condition . ' GROUP BY ', $sqlTotal);
			else
				$sqlTotal = str_replace(' ORDER BY ', ' AND ' . $condition . ' ORDER BY ', $sqlTotal);
		}
		else
		{
			if(strpos($sqlTotal, 'GROUP BY') !== FALSE)
				$sqlTotal = str_replace(' GROUP BY ', ' WHERE ' . $condition . ' GROUP BY ', $sqlTotal);
			else
			$sqlTotal = str_replace(' ORDER BY ', ' WHERE ' . $condition . ' ORDER BY ', $sqlTotal);
		}
//print "$sqlTotal<P>";
		return $sqlTotal;
	}
// Grab all resource IDs in query and write to session for use in 'previous' and 'next' links 
// when viewing a single resource. 
// Also, remove elements of $this->pagingArray where there are no resources
	function grabTotal($sqlTotal, $order)
	{
		if($order == 'creator')
			$tempPagingArray = array_fill(0, 28, '');
		else
			$tempPagingArray = array_fill(0, 27, '');
		$recordset = $this->db->query($sqlTotal);
		$this->total = $this->db->numRows($recordset);
		while($row = $this->db->loopRecordSet($recordset))
		{
			$allIds[] = $row['resourceId'];
			if($order == 'title')
				$char = strtoupper(substr(str_replace('}', '', str_replace('{', '', $row['title'])), 0, 1));
			else if($order == 'creator')
			{
				if($row['surname'])
					$char = strtoupper(substr($row['surname'], 0, 1));
				else
				{
					$tempPagingArray[0] = '--';
					continue;
				}
			}
			if(array_search($char, $tempPagingArray) !== FALSE) // already noted
				continue;
			if(($index = array_search($char, $this->pagingArray)) !== FALSE)
			{
				$tempPagingArray[$index] = $this->pagingArray[$index];
				continue;
			}
// If we get here, we have a creator or title with an English character initially
			$tempPagingArray[sizeof($tempPagingArray) - 1] = '??';
		}
		foreach($tempPagingArray as $index => $element)
		{
			if(!$element)
				unset($this->pagingArray[$index]);
		}
		if(isset($allIds))
			$this->session->setVar('list_allIds', base64_encode(serialize($allIds)));
		else
			$this->session->delVar("list_allIds");
	}
// Links at bottom of display screens to move to more resources.
// $this->total is total resources found for this operation.
// $this->start is where we currently are.
	function links($queryString)
	{
		if(sizeof($this->pagingArray) <= 1)
			return FALSE;
		include_once("core/html/MISC.php");
		foreach($this->pagingArray as $index => $char)
		{
			if($this->start == $index)
				$links[] = $char;
			else
			{
				$link = htmlentities($queryString . "&pagingStart=$index");
				$links[] = MISC::a("page", "&nbsp;&nbsp;$char&nbsp;&nbsp;", "index.php?" . $link);
			}
		}
		if($this->session->getVar($this->listType . '_ascDesc') == 'DESC')
			$links = array_reverse($links);
		return implode("&nbsp;|&nbsp;", $links);
	}
// Format display information string
	function linksInfo($bibTitle = FALSE)
	{
		if(!$this->total)
			return $this->messages->text("resources", "noResult");
		$bib = FALSE;
		if(sizeof($this->pagingArray) == 1)
			$num = $this->total;
		else
			$num = "'" . $this->pagingArray[$this->start] . "'";
		if($bibTitle)
		{
			$html = new HTML();
			$bib = " (" . $this->messages->text("user", "bibliography") . ": " . 
			$html->dbToHtmlTidy($bibTitle) . ")";
		}
		else if($this->session->getVar('setup_multiUser'))
			$bib = " (" . $this->messages->text("user", "bibliography") . ": " . 
				$this->messages->text("user", "masterBib") . ")";
		return $this->messages->text("hint", "pagingInfo", 
			" $num of " . $this->total . $bib);
	}
}
?>
